package com.openecommerce.api.domain.basic;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 用于从父类中获取子类对象并包含父类数据
 * @author Tommy
 *
 * @param <T>
 */
public abstract class ChildObjectGenerator<T> {
	
    private static final Logger logger = LoggerFactory.getLogger(ChildObjectGenerator.class);
	
	/**
	 * 为子类对象填充数据(注意：子类对象调用该方法则复制对象属性值)
	 * @param object
	 */
	public void fillChildObject(T object){
	    Method[] methods = this.getClass().getDeclaredMethods();
	    List<Method> methodList = Arrays.asList(methods);
	    for(Method method : methodList){
	    	if(method.getName().startsWith("get")){
	    		try {
	    			object.getClass().getMethod(method.getName().replaceFirst("get", "set"), method.getReturnType()).invoke(object,method.invoke(this));
				} catch (Exception e) {
					logger.error("父类获取子类数据异常", e);
				}
	    	}
	    }
	}
	
}